#include "leetcode/leetcode.h"

class Solution {
public:

    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        if(n == 1) return;

        for(int start = 0; start <n/2; start++) {

            int row=start, col=start;
            int swaped_row=n-start-1, swaped_col=start;

            for(int i = 0; i < n-1-start*2; i++){
                swap(matrix[row][col], matrix[swaped_row][swaped_col]);
                row++;
                swaped_col++;
            }
            for(int i = 0; i < n-1-start*2; i++){
                swap(matrix[row][col], matrix[swaped_row][swaped_col]);
                col++;
                swaped_row--;
            }
            for(int i = 0; i < n-1-start*2; i++){
                swap(matrix[row][col], matrix[swaped_row][swaped_col]);
                row--;
                swaped_col--;
            }
        }
    }
};